<?php 
include('common_include.php');
include('validateSession.php');

$prodCategory = $_POST['prodCategory'];
$prodSubcategory = $_POST['prodSubcategory'];
$prodName = $_POST['prodName'];
$formAction = $_POST['formAction'];

if ($formAction == "Search") {
	$prodArr = array();
	$dao = new SQLitePersistence();
	$prodArr = $dao->getProductsByCategory($prodCategory, $prodSubcategory, $prodName);
	unset($dao);
} else if ($formAction == "Defunct") {
	$prodId = $_POST['prodId'];
	$dao = new SQLitePersistence();
	$dao->defunctProductsById($prodId);
	$prodArr = $dao->getProductsByCategory($prodCategory, $prodSubcategory, $prodName);
	unset($dao);
}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script language="javascript" src="scripts/common.js"></script>
<title>
	Browse Product
</title>
<script>

function submitForSearch() {
	document.browseProductForm.action = "product_browse.php";
	document.browseProductForm.formAction.value = "Search";
	if (document.browseProductForm.prodCategory.value == "") {
		alert("Product Category is mandatory search criteria!");
		return;
	};
	document.browseProductForm.submit();
}

function submitForEdit(prodId) {
	document.browseProductForm.action = "product_update.php?prodId="+prodId+"&pageAction=UpdInit";
	document.browseProductForm.formAction.value = "UpdInit";
	document.browseProductForm.prodId.value = prodId;
	document.browseProductForm.submit();
}

function submitForDefunct(prodId) {
	document.browseProductForm.action = "product_browse.php";
	document.browseProductForm.formAction.value = "Defunct";
	document.browseProductForm.prodId.value = prodId;
	document.browseProductForm.submit();
}

</script>
</head>
<body>

<form name="browseProductForm" method="post">
<input type="hidden" name="formAction">
<input type="hidden" name="prodId">
<table>
	<tr>
		<td class="errorLabel">
			<?php echo $_GET['error']; ?>
		</td>
	</tr>
	<tr>
		<td class="labelClass">
			Browsing Criteria:
		</td>
	</tr>
	<tr>
		<td>
		<table>
			<tr>
				<td class="textLabelClass">Product Category</td>
				<td class="textLabelClass">Subcategory</td>
				<td class="textLabelClass">Product Name</td>
			</tr>
			<tr>
				<td>
					<select name="prodCategory">
					  <option value="">-------- Please Select --------</option>
					  <?php
					  	if ($prodCategory == "Winter Accessories") {
					  ?>
					  		<option value="Winter Accessories" selected>Winter Accessories</option>
					  <?php
					  	} else {
					  ?>
					  		<option value="Winter Accessories">Winter Accessories</option>
					  <?php
					  	}
					  	
					  	if ($prodCategory == "Bags & Luggage") {
					  ?>
					  		<option value="Bags & Luggage" selected>Bags & Luggage</option>
					  <?php
					  	} else {
					  ?>
					  		<option value="Bags & Luggage">Bags & Luggage</option>
					  <?php
					  	}
					  	
					  	if ($prodCategory == "Casual Wear") {
					  ?>
					  		<option value="Casual Wear" selected>Casual Wear</option>
					  <?php
					  	} else {
					  ?>
					  		<option value="Casual Wear">Casual Wear</option>
					  <?php
					  	}
					  	
					  	if ($prodCategory == "Outdoor & Camping Products") {
					  ?>
					  		<option value="Outdoor & Camping Products" selected>Outdoor & Camping Products</option>
					  <?php
					  	} else {
					  ?>
					  		<option value="Outdoor & Camping Products">Outdoor & Camping Products</option>
					  <?php
					  	}
					  ?>
					</select>
				</td>
				<td>
					<input type="text" name="prodSubcategory" value="<?php echo $prodSubcategory?>" />
				</td>
				<td>
					<input type="text" name="prodName" value="<?php echo $prodName?>" />
				</td>					
			</tr>
			<tr>
				<td colspan="3"><input type="button" name="btnSearch" value="Search" onclick="submitForSearch();"/></td>
			</tr>
		</table>
		</td>
	</tr>
	<tr><td>&nbsp;</td></tr>
	<tr><td>&nbsp;</td></tr>
	<tr>
		<td class="labelClass">
			Product Listing:
		</td>
	</tr>
	<tr>
		<td class="labelClass">
			<table class="listingTable"> 
				<tr class="yellow">
					<td class="dataheader">Product Code</td>
					<td class="dataheader">Subcategory</td>
					<td class="dataheader">Product Name</td>
					<td class="dataheader">Unit Price</td>
					<td class="dataheader">Post Allow</td>
					<td class="dataheader">Defunct</td>
					<td class="dataheader">Edit</td>
				</tr>
				<?php for ($i=0; $i < sizeof($prodArr); $i++) {
					$rowIDAttr = $i;
					$aProduct = $prodArr[$i];
					$submitForDefunctOnClickEvent = 'submitForDefunct(' . $aProduct->getProductId() . ');';
					$submitForEditOnClickEvent = 'submitForEdit(' . $aProduct->getProductId() . ');';
				?>
				<tr class="datarow">
					<td class="dataleft"><?php echo $aProduct->getProductCode();?></td>
					<td class="dataleft"><?php echo $aProduct->getSubcategory();?></td>
					<td class="dataleft"><?php echo $aProduct->getProductName();?></td>
					<td class="dataright"><?php echo $aProduct->getUnitPrice();?></td>
					<td class="datacenter"><?php echo $aProduct->getPostAllow();?></td>
					<td class="datacenter">
						<img src="images/del.jpg" width="18" height="18" onclick="<?php echo $submitForDefunctOnClickEvent?>" />
					</td>
					<td class="datacenter">
						<img src="images/edit.jpg" width="18" height="18" onclick="<?php echo $submitForEditOnClickEvent?>" />
					</td>
				</tr>
				<?php } 
				unset($prodArr);?>
			</table>
		</td>
	</tr>
</table>
</form>

</body>
</html>